import { _decorator, Component, Node, Collider, ICollisionEvent, RigidBody, v3 } from 'cc';
const { ccclass, property } = _decorator;

const tmp_V1 = v3();
const tmp_V2 = v3();

@ccclass('BallCollision')
export class BallCollision extends Component {
    public static collideWithBalls:number = 0;
    start() {
        this.getComponent(Collider).on("onCollisionEnter", this.onCollisionEnter);
    }

    onCollisionEnter(event:ICollisionEvent){
        let name = event.otherCollider.node.name;
        if(name == 'TablePlane' || name == 'table_side' || name == 'table'){
            return;
        }
        event.selfCollider.node.getComponent(RigidBody).getLinearVelocity(tmp_V1);
        event.otherCollider.node.getComponent(RigidBody).getLinearVelocity(tmp_V2);
        let max = tmp_V1.length() + tmp_V2.length();
        if(max > BallCollision.collideWithBalls){
            BallCollision.collideWithBalls = max;
        }
    }
}

